package com.example.core.upload;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @ClassName 文件上传下载
 * @Author zhangzhaoyang
 * @Date 2019/9/27 15:41
 **/
public class FileAdminUtils {
    private static Logger logger = LoggerFactory.getLogger(FileAdminUtils.class);
    private static String filePath="D:/1111111";


    public static String multifileUpload(MultipartFile file, String newFileName){
        if(file.isEmpty()){
            return "false";
        }
//        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();
        logger.info("文件名字=="+newFileName+"==文件大小=="+size);
//        log.info("文件名字=="fileName + "-->" + size);

        String path = filePath;
        File dest = new File(path + "/" + newFileName);
        //判断文件父目录是否存在
        if(!dest.getParentFile().exists()){
            dest.getParentFile().mkdir();
        }
        try {
            //保存前先删除重复文件
            String del_result=delFile(newFileName);
            logger.info("删除结果=="+del_result);
//保存文件
            file.transferTo(dest);
            return "true";
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        }
    }
    //    删除文件
    public static String delFile(String fileName) {
        String resultInfo=null;
        String sb=filePath+"/"+fileName;
        File file = new File(sb);
        if (file.exists()) {
            if (file.delete()) {
                resultInfo =  "1-删除成功";
            } else {
                resultInfo =  "0-删除失败";
            }
        } else {
            resultInfo = "文件不存在！";
        }

        return resultInfo;
    }
    //文件下载
    public static String  downLoad(HttpServletResponse response, String filename){
        File file = new File(filePath + "/" + filename);
        //判断文件父目录是否存在
        if(file.exists()){
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            // response.setContentType("application/force-download");
            try {
                response.setHeader("Content-Disposition", "attachment;fileName=" +   java.net.URLEncoder.encode(filename,"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            byte[] buffer = new byte[1024];
            //文件输入流
            FileInputStream fis = null;
            BufferedInputStream bis = null;
//输出流
            OutputStream os = null;
            try {
                os = response.getOutputStream();
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                int i = bis.read(buffer);
                while(i != -1){
                    os.write(buffer);
                    i = bis.read(buffer);
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("----------file download---" + filename);
            try {
                bis.close();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }



}
